package distriKub;

import java.util.EnumSet;

public class Bank {
	private int seniority = 0; // Rise by +1 each time the player finishes his turn
	
	public final static int NUMBEROFTILES = 106;
	//private Tile[] gameBank = new Tile[NUMBEROFTILES];
	public Tile[] gameBank = new Tile[NUMBEROFTILES]; // TODO : Should be private !
	
	public void resetGameBank() // Tested OK
	{
		int placeInBank = 0;
		for(int pairOrder=0; pairOrder<2;pairOrder++)
		{
			for (int tileColor=0; tileColor<4; tileColor++)
				for (int tileNumber=1;tileNumber<14;tileNumber++)
			{
					Tile.tileColors currentColor = Tile.tileColors.Black; // Default Init
					
					for (Tile.tileColors enumColor : EnumSet.range(Tile.tileColors.Black, Tile.tileColors.Red))
						if (enumColor.ordinal()==tileColor) currentColor=enumColor;
					
				gameBank[placeInBank] = new Tile(currentColor,tileNumber);
				placeInBank++;
			}
			
			gameBank[placeInBank] = new Tile(Tile.tileColors.Joker,Tile.JOKERNUMBER);
			placeInBank++;
		}
	}
	
	// Format : Color,Number,OwnerID,ScreenName,Position
	@Override public String toString()
	{
		StringBuilder result = new StringBuilder();
		for (Tile currentTile : gameBank)
		{
			result.append(currentTile.getTileColor().toString() + ",");
			result.append(String.valueOf(currentTile.getTileNumber()) + ",");
			result.append(String.valueOf(currentTile.getTileOwnerId()) + ",");
			result.append(currentTile.getOwnerScreenName() + ",");
			result.append(String.valueOf(currentTile.getTilePosition()) + ",");
		}
		return result.toString().substring(0, result.length()-1);
	}
	
	public void raiseSeniority(){
		this.seniority++;
	}
	
	public int getSeniority(){
		return this.seniority;
	}
	
	public void setSeniority(int theSeniority){
		this.seniority = theSeniority;
	}
	
	// Gets a toString() of another bank and sets gameBank accordingly
	public void bankFromString(String bankString)
	{ // Tested OK
		String[] parsedInput = bankString.split(",");
		int positionInParsedInput=0;
		
		Tile.tileColors currentColor;
		int currentNumber;
		int currentOwnerId;
		String currentScreenName;
		int currentPosition;
		
		for (int positionInBank=0; positionInBank<106; positionInBank++)
		{
			gameBank[positionInBank] = new Tile(); // Intended invalid Initialization.
			
			currentColor = Tile.tileColors.valueOf(parsedInput[positionInParsedInput++]);
			currentNumber = Integer.parseInt(parsedInput[positionInParsedInput++]);
			currentOwnerId = Integer.parseInt(parsedInput[positionInParsedInput++]);
			currentScreenName = parsedInput[positionInParsedInput++];
			currentPosition = Integer.parseInt(parsedInput[positionInParsedInput++]);
			
			gameBank[positionInBank].setTileColor(currentColor);
			gameBank[positionInBank].setTileNumber(currentNumber);
			gameBank[positionInBank].setTileOwnerId(currentOwnerId);
			gameBank[positionInBank].setOwnerScreenName(currentScreenName);
			gameBank[positionInBank].setTilePosition(currentPosition);
		}
	}
	
	public Bank(){ // Default Constructor
		this.resetGameBank();
	}

}
